<?php

/**
 * This is the model class for table "checks".
 *
 * The followings are the available columns in table 'checks':
 * @property integer $checks_id
 * @property integer $checks_user_id
 * @property integer $checks_tests_id
 * @property string $checks_create_time
 * @property string $checks_submit_time
 * @property integer $checks_status
 * @property integer $checks_score
 * @property integer $checks_rank
 */
class Checks extends CActiveRecord
{
	const CHECKUP = 1;
	const CHECKDOWN = 2;
	public static function getStateArr()
	{
		return array(
			self::CHECKUP => '未提交',
			self::CHECKDOWN =>'已提交',
		);
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Checks the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'checks';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('checks_user_id, checks_tests_id, checks_status, checks_score, checks_rank', 'numerical', 'integerOnly'=>true),
			array('checks_create_time, checks_submit_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('checks_id, checks_user_id, checks_tests_id, checks_create_time, checks_submit_time, checks_status, checks_score, checks_rank', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author'=>array(self::BELONGS_TO,'User','checks_user_id'),
			'exams'=>array(self::BELONGS_TO,'Exams','checks_tests_id'),
			'answers'=>array(self::HAS_MANY,'Answers','answers_checks_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'checks_id' => '考生考试',
			'checks_user_id' => '考生姓名',
			'checks_tests_id' => '考试名称',
			'checks_create_time' => '参考时间',
			'checks_submit_time' => '提交时间',
			'checks_status' => '考试状态',
			'checks_score' => '考试成绩（分）',
			'checks_rank' => '考试排名',
			'exams.exams_name' => '考试名称',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('checks_id',$this->checks_id);

		$criteria->compare('checks_user_id',$this->checks_user_id);

		$criteria->compare('checks_tests_id',$this->checks_tests_id);

		$criteria->compare('checks_create_time',$this->checks_create_time,true);

		$criteria->compare('checks_submit_time',$this->checks_submit_time,true);

		$criteria->compare('checks_status',$this->checks_status);

		$criteria->compare('checks_score',$this->checks_score);

		$criteria->compare('checks_rank',$this->checks_rank);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	/**
	 * 更新指定考试的排名
	 * @param $testsId
	 */
	public function updateRang($testsId) {
		$rs = $this->findAll('checks_tests_id = :checks_tests_id',array(':checks_tests_id'=>$testsId));
		if($rs)
		{
			$i = 1;
			foreach ($rs as $v) {
				$v->checks_rank = $i++;
				$v->save();
			}
		}
		return true;
	}
}